# THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),  
# to deal in the Software without restriction, including without limitation  
# the rights to use, copy, modify, merge, publish, distribute, sublicense,  
# and/or sell copies of the Software, and to permit persons to whom the  
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in 
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS IN THE SOFTWARE.

# topbar.py
# written by: 
#     Jan Jokela <jan@devion.org>

import os, sys
from qgl import scene
from fluid.eye import *
import fluid.actions as actions
import base.config.config as config
import base.config.data as data
import time

class TopBar(object):
    
    def __init__(self, parent):
        self.parent = parent
        eventhandler.subscribe(self)
        self.now = (0,0,0,0,0)
        self.color = qgl.scene.state.Color((1.0,1.0,1.0,0.0))
        self.color_mouse = qgl.scene.state.Color((1.0,1.0,1.0,0.0))
        self.draw()
        self.drawclock()
        
    def draw(self):  
        img = os.path.join(data.theme, 'topbar.png')
        tex = qgl.scene.state.Texture(img)
        quad = qgl.scene.state.Quad((68,4))
        self._w_topbar = qgl.scene.Group()
        self._w_topbar.add(tex, self.color, quad)
        self._w_topbar.translate = (0.0,19.1,-50.0)
        viewport.add(self._w_topbar)
        
        img = os.path.join(data.theme, 'menu.png')
        tex = qgl.scene.state.Texture(img)
        quad = qgl.scene.state.Quad((4,4))
        self._w_devion_logo = qgl.scene.Group()
        self._w_devion_logo.add(tex, self.color_mouse, quad)
        self._w_devion_logo.translate = (-31.0,18.7,-50.0)
        self._w_devion_logo.selectable = True
        viewport.add(self._w_devion_logo)
        
        img = os.path.join(data.theme, 'volume_up.png')
        tex = qgl.scene.state.Texture(img)
        quad = qgl.scene.state.Quad((4,4))
        self._w_vol_up = qgl.scene.Group()
        self._w_vol_up.add(tex, self.color_mouse, quad)
        self._w_vol_up.translate = (14.0,18.7,-50.0)
        self._w_vol_up.selectable = True
        viewport.add(self._w_vol_up)
        
        img = os.path.join(data.theme, 'volume_down.png')
        tex = qgl.scene.state.Texture(img)
        quad = qgl.scene.state.Quad((4,4))
        self._w_vol_down = qgl.scene.Group()
        self._w_vol_down.add(tex, self.color_mouse, quad)
        self._w_vol_down.translate = (18.0,18.7,-50.0)
        self._w_vol_down.selectable = True
        viewport.add(self._w_vol_down)
        
        actions.Fade(self.color, alpha=1.0, time=3.0)
        if mousehandler.active(): actions.Fade(self.color_mouse, alpha=1.0, time=.5)
        
    def drawclock(self):
        now = time.localtime(time.time())
        if now[:5] != self.now[:5]:
            if self.now[:5] != (0,0,0,0,0): 
                viewport.remove(self._w_clock_hours)
                viewport.remove(self._w_clock_minutes)
                viewport.remove(self._w_clock_ampm)
            self.now = now
            """quad = qgl.scene.state.Quad((4,4))
            #with secnds: text = qgl.scene.state.Text(time.strftime("%I:%M:%S %p", now), "FreeSans.ttf", (0.8, 0.8, 0.8), None, 32)
            text = qgl.scene.state.Text(time.strftime("%I:%M %p", self.now), data.fonts + "/URWGothicL.ttf", (0.8, 0.8, 0.8), None, 32)
            self._w_clock = qgl.scene.Group()
            self._w_clock.add(self.color, text)
            self._w_clock.translate = (10.5,8.7,-25.0)
            viewport.add(self._w_clock)"""
            
            quad = qgl.scene.state.Quad((4,4))
            text = qgl.scene.state.Text(time.strftime("%I", self.now), data.fonts + "/URWGothicL.ttf", (0.8, 0.8, 0.8), None, 32)
            self._w_clock_hours = qgl.scene.Group()
            self._w_clock_hours.add(self.color, text)
            self._w_clock_hours.translate = (12,8,-23.0)
            viewport.add(self._w_clock_hours)
            
            quad = qgl.scene.state.Quad((4,4))
            text = qgl.scene.state.Text(time.strftime("%M", self.now), data.fonts + "/URWGothicL.ttf", (0.8, 0.8, 0.8), None, 24)
            self._w_clock_minutes = qgl.scene.Group()
            self._w_clock_minutes.add(self.color, text)
            self._w_clock_minutes.translate = (20.5,12.2,-35.0)
            viewport.add(self._w_clock_minutes)
            
            quad = qgl.scene.state.Quad((4,4))
            text = qgl.scene.state.Text(time.strftime("%p", self.now), data.fonts + "/URWGothicL.ttf", (0.8, 0.8, 0.8), None, 24)
            self._w_clock_ampm = qgl.scene.Group()
            self._w_clock_ampm.add(self.color, text)
            self._w_clock_ampm.translate = (20.5,13.1,-35.0)
            viewport.add(self._w_clock_ampm)
        
    def handle_events(self, events):
        self.drawclock()
        for event in events:
            if config.input == "keyboard + mouse": 
                if event.type == 'mouse_active':
                    actions.Fade(self.color_mouse, alpha=1.0, time=.5)
                elif event.type == 'mouse_inactive':
                    actions.Fade(self.color_mouse, alpha=0.0, time=.5)
            if (config.input == "keyboard + mouse" and mousehandler.active()) or config.input == "touchscreen": 
                if event.type == MOUSEBUTTONDOWN:
                    picker.set_position(event.pos)
                    root_node.accept(compiler)
                    root_node.accept(picker)          
                    if self._w_devion_logo in picker.hits:
                        self.parent.destroy()
                        import base.menu as menu
                        menu.MainMenu()
                        root_node.accept(compiler)
                    if self._w_vol_down in picker.hits:
                        music_data.audio_player.decrement_volume(0)
                        
        
    def destroy(self):
        eventhandler.unsubscribe(self)
        viewport.remove(self._w_topbar)
        viewport.remove(self._w_devion_logo)
        viewport.remove(self._w_vol_up)
        viewport.remove(self._w_vol_down)
        viewport.remove(self._w_clock_hours)
        viewport.remove(self._w_clock_ampm)
        viewport.remove(self._w_clock_minutes)
          